package com.smartmedical.auth.doctor.service;

import com.smartmedical.auth.doctor.doctordomain.entity.DoctorEnity;
import com.smartmedical.auth.doctor.util.RSAUtils;
import com.smartmedical.auth.doctor.util.RsaKey;
import com.smartmedical.common.core.domain.R;
import com.smartmedical.common.core.exception.ServiceException;
import com.smartmedical.common.core.utils.StringUtils;
import com.smartmedical.system.api.RemoteUserService;
import com.smartmedical.system.api.domain.Doctor;
import com.smartmedical.system.api.model.LoginDoctor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

@Component
public class DoctorService {

    @Autowired
    private RemoteUserService remoteUserService;

    public DoctorEnity docLogin(String email, String pwd) throws Exception {
        if (StringUtils.isAnyBlank(email, pwd)) {
            throw new RuntimeException("email or pwd is null");
        }
        R<LoginDoctor> userResult = remoteUserService.getDoctorInfo(email);

        if (!emailistrue(email)) {
            throw new RuntimeException("邮箱格式错误");   // 业务异常
        }
        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
        {
            throw new RuntimeException("登录邮箱：" + email + " 不存在");
        }

        LoginDoctor data = userResult.getData();

        try {
            String password = RSAUtils.rsaDecrypt(pwd, RsaKey.PRIVATE_KEY);

            if (!data.getSysUser().getPwd().equals(password)) {
                throw new RuntimeException("密码错误,请重新输入");
            }

            Doctor sysUser = data.getSysUser();
            //赋值
            DoctorEnity doctor = createDoctorEnity(sysUser);

            return doctor;

        }catch (Exception e) {
            throw new ServiceException("公钥或私钥错误");// 业务异常
        }




    }


    private boolean emailistrue(String email) {


        Pattern compile = Pattern.compile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
        boolean matches = compile.matcher(email).matches();
        return matches;

    }

    private DoctorEnity createDoctorEnity(Doctor sysUser) {
        DoctorEnity doctor = new DoctorEnity();
        doctor.setId(sysUser.getId());
        doctor.setDepartmentId(sysUser.getDepartmentId());
        doctor.setEmail(sysUser.getEmail());
        doctor.setUserName(sysUser.getUserName());
        doctor.setReviewStatus(sysUser.getReviewStatus());
        doctor.setPhone(sysUser.getPhone());
        doctor.setPhone(sysUser.getPhone());
        doctor.setPwd(sysUser.getPwd());
        doctor.setName(sysUser.getName());
        doctor.setImagePic(sysUser.getImagePic());
        doctor.setJobTitle(sysUser.getJobTitle());
        doctor.setInauguralHospital(sysUser.getInauguralHospital());
        doctor.setPersonalProfile(sysUser.getPersonalProfile());
        doctor.setGoodField(sysUser.getGoodField());
        doctor.setCreateTime(sysUser.getCreateTime());

        return doctor;
    }
}
